package com.ylfstu.encap;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Joy");
        person.setAge(121);
        person.setSalary(30000);
        person.Info();

        //如果直接使用构造器指定属性
        Person smith = new Person("smith", 2000, 60000);
        smith.Info();
    }
}

class Person {
    public String name;
    private int age;
    private double salary;

    public Person() {
    }

    public Person(String name, int age, double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        this.setName(name);
        this.setAge(age);
        this.setSalary(salary);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() >= 2 && name.length() <= 6) {
            this.name = name;
        } else {
            System.out.println("输入的name不正确");
            this.name = "无名";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //判断
        if (age >= 0 && age <= 120) {
            this.age = age;
        } else {
            System.out.println("你年龄设置不对，需在1~120之间，默认给你18");
            this.age = 18;
        }

    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void Info() {
        System.out.println("name=" + this.name + "age=" + this.age + "salary=" + this.salary);
    }
}